<?php
/**
 * Admin Service
 * User: zng
 * Date: 2019/11/4
 * Time: 14:34
 */
namespace app\admin\service;

use app\admin\model\admin\PermissionCategory as PermissionCategoryModel;
use app\exceptions\OrdinaryException;
use think\Exception;

Class PermissionCategory {

    /**
     * 首页列表
     * @param array $get 筛选条件
     * @author zx
     * @date 2019-11-12
     * @return \think\Paginator
     */
    public function indexPaginate( array $get=[] )
    {
        $map = [];
        if ( get($get, 'name') ) {
            $map[] = ['name', 'LIKE', $get['name'].'%'];
        }
        if ( get($get, 'status') === 0 || get($get, 'status') === '0' || get($get, 'status') == 1 ) {
            $map[] = ['status', '=', $get['status']];
        }
        if ( get($get, 'start_create_at') ) {
            $map[] = ['create_time', '>=', strtotime($get['start_create_at'].' 00:00:00')];
        }
        if ( get($get, 'end_create_at') ) {
            $map[] = ['create_time', '<=', strtotime($get['end_create_at'].' 23:59:59')];
        }
        $pageSize = get($get,'pageSize')>0?$get['pageSize']:20;
        return PermissionCategoryModel::where($map)
            ->order('id', 'desc')
            ->paginate($pageSize);
    }

    /**
     * 状态更改
     * @author zx
     * @date 2019-11-14
     * @return \think\response
     */
    public function status(PermissionCategoryModel $permissionCategoryModel)
    {
        $status = $permissionCategoryModel->status==1?2:1;
        $permissionCategoryModel->status = $status;
        $update = $permissionCategoryModel->save();
        if ( $update ) {
            return success(['status'=>$status]);
        }
        return failed('更新失败');
    }

    /**
     * 判断是否唯一
     * @param array $data
     * @author zx
     * @date 2019-11-14
     * @return boolean
     */
    public function only(array $data)
    {
        $map = [];
        $id = get($data, 'id', 0);
        if ( get($data, 'name') ) {
            $map[] = ['name', '=', $data['name']];
        }
        if ( $id <= 0 ) {
            $count = PermissionCategoryModel::where($map)->count();
            if ( $count > 0 ) {
                return false;
            }
        } else {
            $idResult = PermissionCategoryModel::where($map)->column('id');
            foreach ( $idResult as $_id ) {
                if ( $_id != $id ) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 新增\编辑分组信息
     * @param array $data 数据包
     * @author zx
     * @date 2019-11-14
     * @throws OrdinaryException
     * @return boolean
     */
    public function create(array $data)
    {;
        try{
            app('rbac')->savePermissionCategory($data);
        } catch ( Exception $exception ) {
            throw new OrdinaryException($exception->getMessage());
        }
        return true;
    }

    /**
     * 获取分组信息
     * @param mixed $param 条件
     * @author zx
     * @date 2019-11-15
     * @throws OrdinaryException
     * @return mixed
     */
    public function get( $param )
    {
        try{
            $data = app('rbac')->getPermissionCategory( $param );
        } catch ( Exception $exception ) {
            throw new OrdinaryException($exception->getMessage());
        }
        return $data;
    }

    /**
     * 批量删除
     * @param mixed $data
     * @author zx
     * @date 2019-11-15
     * @throws OrdinaryException
     * @return boolean
     */
    public function destory( $data )
    {
        try{
            $bool = app('rbac')->delPermissionCategory( $data );
        } catch ( Exception $exception ) {
            throw new OrdinaryException($exception->getMessage());
        }
        return $bool;
    }

    /**
     * 获取分组path列表
     * @return array
     */
    public function getPath()
    {
        return PermissionCategoryModel::status()->column('path');
    }


    /**
     * 获取登录用户的权限分组
     * @param array $userInfo 登录信息
     */
    public function getPermissionCategoryByUserInfo( array $userInfo )
    {
        return PermissionCategoryModel::permissionCategoryByUserId($userInfo['id'])->column('permission_category.id');
    }

}